
import apiCaller from './ajax/ApiCaller'
import * as qiniu from 'qiniu-js'
import {Toast} from "vant";

class LtxService {

    tip(type,msg){
        Toast(msg)
    }

    uploadRes(files,apiId){
        let datas = []
        let policies = []
        let srcNames = []
        for (let i = 0; i < files.length; i++) srcNames.push(files[i].name)
        console.log(files)
        let observer = {
            next(res) {
            },
            error(err) {
                alert(err.message);
            },
            complete(res) {
                alert("OK");
            }
        }

        apiCaller.call({
            url:"applyForUpload",
            method:"GET",
            params:{srcNameArr: srcNames, apiId: apiId}
        }).then(
            function (response) {
                datas = response.data["data"]
                for (let i = 0; i < srcNames.length; i++) {
                    let key = datas[i]['upPolicy']['key']
                    let token = datas[i]['upToken']
                    let putExtra = {
                        fname: srcNames[i],
                        params: {},
                        mimeType: ["image/png", "image/jpeg","image/jpg", "image/gif"]
                    }
                    let config = {
                        useCdnDomain: true,
                        region: qiniu.region.z2
                    };

                    let observable = qiniu.upload(files[i], key, token, putExtra, config)
                    let subscription = observable.subscribe(observer) // 上传开始
                }
            }
        )

    }


    login(userCode,password,captcha)
    {
        return apiCaller.call({url:'user/login' ,
        params: {
            userCode:userCode,
                password:password,
                captcha:captcha}
        })
    }
    register(user)
    {
        return apiCaller.call({url:'user/register' ,
            params:user
        })
    }

    getUserRes(resDir)
    {
        return apiCaller.call({url:"userRes",params:{resDir:resDir}})
    }

    getUserResArr(ids)
    {

        return apiCaller.call({url:"userResArr",params:{ids:ids}})
    }

    getMsgs(msgType,isRead)
    {
        return apiCaller.call({url:"userMsgs",params:{msgType:msgType,isRead:isRead}})
    }
    deleteMsgs(msgIds)
    {
        return apiCaller.call({url:"deleteUserMsgs",method:"POST",params:{msgIds:msgIds}})
    }
    readMsgs(msgIds)
    {
        return apiCaller.call({url:"readUserMsgs",method:"POST",params:{msgIds:msgIds}})
    }

    addPaper(paper)
    {
        return apiCaller.call({url:"addPaper",method:"POST",body:paper})
    }

    previewNew(page)
    {
        console.log(page)
        return apiCaller.call({url:"new",params:{page:page}})
    }

    previewHot(page)
    {
        console.log(page)
        return apiCaller.call({url:"hot",params:{page:page}})
    }

    getPaperDetail(paperId)
    {
        return apiCaller.call({url:"paperDetail",params:{paperId:paperId}})
    }

    getComments(paperId)
    {
        return apiCaller.call({url:"comments",params:{paperId:paperId}})
    }
    addComment(comment)
    {
        return apiCaller.call({url:"addComment",params:comment})
    }

    setPaperAttitude(paperId,emotion)
    {
        return apiCaller.call({url:"setAttitude",method:"POST",params:{
                paperId:paperId,emotion:emotion
            }})
    }

    updateUserInfo(userInfo)
    {
        return apiCaller.call({url:"user/updateInfo",method:"GET",params:userInfo})
    }

    getUserInfo()
    {
        return apiCaller.call({url:"user/getUserInfo",method:"POST"})
    }

    updatePassword(oldPassword,newPassword)
    {
        return apiCaller.call({url:"user/updatePassword",params:{oldPassword:oldPassword,newPassword:newPassword}})
    }


}

export default new LtxService()